/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.Fever;
import fever.log.FeverDebug;
import fever.utils.StringUtil;

import flash.external.ExternalInterface;

/**
 * Track application / site internal navigation throw Google Analytic system.
 * 
 * <p>More informations on http://www.google.com/analytics/
 * 
 * <p>Link passed to tracker is check before processing :<br />
 * At the end, link must start with a {@code /} char with no space chars 
 * at the end.
 * <ul>
 *   <li>"/ /  samplesviewer / / /  " --> "/samplesviewer"</li>
 *   <li>"myLink   " --> "/myLink"</li>
 *   <li>"  samples viewer  " --> "/samples_viewer"</li>
 * </ul>
 * 
 * <p>Analytics tracking code must be placed in your HTML code above any of these calls.<br />
 * Example
 * {@code
 *   //<![CDATA[
 *     <script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script>
 *     
 *	   _uacct = "YOUR_GOOGLE_TRACK_ID";
 *	   urchinTracker();	
 *	 //]]>
 * }
 * 
 * <p>Tracking is enabled if {@link fever.core.CoreApplication#isOnline()} is {@code true}.
 * 
 * <p>Check if {@code ExternalInterface.available}.<br />
 * if {@code true}, use {@code ExternalInterface.call}, otherwise use 
 * {@code getURL( "javascript:"" )}.
 * 
 * @see fever.core.CoreApplication
 * @see fever.utils.StringUtil
 * 
 * @author Romain Ecarnot
 */
class fever.utils.GAnalytic
{
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Default logical directory structure for outbound links. */
	public static var OUTGOING_TRACK : String = 'outgoing';
	
	/** Default logical directory structure for downloads links. */
	public static var DOWNLOAD_TRACK : String = 'downloads';
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Track passed-in {@code link}.
	 */
	public static function track( link : String ) : Void
	{
		_track( _buildCorrectLink( link ) );	
	}
	
	/**
	 * Track passed-in {@code link} as an outbound link.
	 * 
	 * <p>Logical link structure can me modify with {@link #OUTGOING_TRACK} 
	 * property.
	 */
	public static function trackOutBound( link : String ) : Void
	{
		_track( _buildCorrectLink( link, OUTGOING_TRACK ) );
	}
	
	/**
	 * Track passed-in {@code link} as a download link.
	 * 
	 * <p>Logical link structure can me modify with {@link #DOWNLOAD_TRACK} 
	 * property.
	 */
	public static function trackDownload( link : String ) : Void
	{
		_track( _buildCorrectLink( link, DOWNLOAD_TRACK ) );		
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Creates a correct Google Analytic track link.
	 * 
	 * <p>Is correct when link start with a {@code /} char
	 * <p>Adds {@code baseLink} for outbounds or downloads logical directory
	 * <p>Adds {@link fever.core.Application#name} for directory project
	 */
	private static function _buildCorrectLink( link : String, baseLink : String ) : String
	{
		if( link )
		{
			link = '/' + StringUtil.trim( link, StringUtil.EMPTY_CHARS + '/' );
			link = StringUtil.replace( link, ' ', '_' );
			link = StringUtil.replace( link, 'http://', '' );
			link = StringUtil.replace( link, 'https://', '' );
			
			if( baseLink ) link = '/' + baseLink + link;
		}
		else link = '';
		
		var appName : String = Fever.application.name;
		appName = ( appName ) ? '/' + StringUtil.trim( appName, StringUtil.EMPTY_CHARS + '/' ) : '';	
		
		var result : String = appName.toLowerCase() + link; 
		return result;
	}
	
	/**
	 * Call {@code urchinTracker()} javascript method with correct link.
	 * 
	 * <p>Check if {@code ExternalInterface.available}.<br />
	 * if {@code true}, use {@code ExternalInterface.call}, otherwise use 
	 * {@code getURL( "javascript:"" )}.
	 * 
	 * @param link Correct link built with {@link #_buildCorrectLink()}
	 */
	private static function _track( link : String ) : Void
	{
		if( Fever.application.isOnline() && ( link.length > 2 ) )
		{
			if( ExternalInterface.available ) ExternalInterface.call( 'urchinTracker', link);
			else getURL("javascript:urchinTracker('"+ link +"');");
		}
		
		FeverDebug.INFO( 'GoogleAnalytic tracking : ' + link );
	}
	
	/**
	 * Constructor.
	 */
	private function GAnalytic() {}
}